package alloytoy;
import java.io.*;
import java.util.*;


/**
 * Convert a dot file into a Digraph object.
 * @author daniel
 */
public class DotParser{
    String fileName;

    public DotParser(String fName){
        fileName=fName;
    }
    static Map sortByValue(Map map) {
        List list = new LinkedList(map.entrySet());
	Collections.sort(list, new Comparator() {
            public int compare(Object o1, Object o2) {
                return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
          }
        });
	// logger.info(list);
	Map result = new LinkedHashMap();
	for (Iterator it = list.iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry)it.next();
            result.put(entry.getKey(), entry.getValue());
	}

        return result;
    }
    
    public static Map<String,Integer> ConstructMap(Digraph graph, ArrayList<String> sigs ){
        TransitiveClosure trans = new TransitiveClosure(graph);
        Map<String,Integer> finalMap = new HashMap<String,Integer>();
        int count=0;
        for(int i=0; i<graph.V; i++){
            count=0;
            for(int j=0; j<graph.V; j++){
                if(trans.reachable(i,j)) count++;
            }
            finalMap.put(sigs.get(i),count);
        }
        return sortByValue(finalMap);
    }

    /**
    * ideal não é contar o número de arcos mas sim o fecho transitivo
    **/
    public void ComputeCouting(){
        Map<String,Integer> entradas = new HashMap<String,Integer>();
	Map<String,Integer> saidas = new HashMap<String,Integer>();
	Map<String,Integer> edjes = new HashMap<String,Integer>();
	Map<String,Integer> finalMap = new HashMap<String,Integer>();
	Set<String> vertices = new HashSet<String>();
	ArrayList<String> sigs = new ArrayList<String>();
        File file = new File(fileName);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);
            int counter=0;
            while(dis.available() !=0){
                StringTokenizer token = new StringTokenizer(dis.readLine());
		token.nextToken("<");
		String tmp = token.nextToken("<");
		int charat = tmp.indexOf(">");
		String tmp2 = tmp.substring(0,charat);
		StringTokenizer token2 = new StringTokenizer(tmp2);
		String keySaida=token2.nextToken(", ");
		String keyEntrada=token2.nextToken();
		if(!edjes.containsKey(keySaida)){
                    edjes.put(keySaida,counter);
                    sigs.add(counter,keySaida);
                    counter++;
		}
		if(!edjes.containsKey(keyEntrada)){
                    edjes.put(keyEntrada,counter);
                    sigs.add(counter,keyEntrada);
                    counter++;
		}
            }
            //System.out.println(sortByValue(edjes));
            Digraph graph = new Digraph(edjes.size());
            fis.close();
            bis.close();
            dis.close();
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);
            while(dis.available() !=0){
                StringTokenizer token = new StringTokenizer(dis.readLine());
		token.nextToken("<");
		String tmp = token.nextToken("<");
		int charat = tmp.indexOf(">");
		String tmp2 = tmp.substring(0,charat);
		StringTokenizer token2 = new StringTokenizer(tmp2);
		String keySaida=token2.nextToken(", ");
		String keyEntrada=token2.nextToken();
		graph.insert(edjes.get(keySaida),edjes.get(keyEntrada));
	  }
	  //graph.printGraph();

          System.out.println(ConstructMap(graph,sigs));
          fis.close();
          bis.close();
          dis.close();
    } catch (FileNotFoundException e) {
        System.out.println(e);
    } catch (IOException e) {
        System.out.println(e);
    }
  }
}